﻿using NewsPortal.Web.Infrastructure.Modularity.ModuleControllerFactory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace NewsPortal.Web.Infrastructure.Modularity
{
    public static class GlobalModuleControllerCollection
    {
        private static Dictionary<string, Controller> _controller;
       
        public static Controller Get(string moduleName)
        {
            if(!IsValid())
            {
                return null;
            }

            return _controller[moduleName];
        }

        public static void Populate(IModuleControllerFactory factory)
        {
            if (_controller == null)
            {
                _controller = new Dictionary<string, Controller>();
            }
            
            GlobalModuleCollection.All().ForEach(mod => _controller.Add(mod.Name, factory.CreateController(mod)));
        }
     
        public static void Remove(string moduleName)
        {
            if(IsValid() && _controller.Keys.Any(k => k == moduleName))
            {
                _controller.Remove(moduleName);
            }
        }

        public static bool IsValid()
        {
            if (_controller == null || _controller.Count <= 0)
            {
                return false;
            }
            return true;
        }
    }
}